﻿using System;
using System.Web;
using System.Text;
using Project.Tool;
using System.Collections.Generic;

namespace Project.Tool
{
    public sealed class CookieHelper
    {
		/// <summary>
		/// 新增Cookie
		/// </summary>
		/// <param name="info">标题和关键字 [Project.Name]</param>
		/// <param name="val">内容</param>
		public static void Add(string info, object val)
		{
			Add(info,val,DateTime.Now.AddDays(7));
		}

		/// <summary>
		/// 新增Cookies
		/// </summary>
		/// <param name="name">项目名</param>
		/// <param name="listCont">键，值 [key,value]</param>
		public static void Add(string info, Dictionary<string, string> listCont)
		{
			Add(info, listCont, DateTime.Now.AddDays(7));
		}

		/// <summary>
		/// 新增Cookie
		/// </summary>
		/// <param name="info">标题和关键字 [Project.Name]</param>
		/// <param name="val">内容</param>
		/// <param name="time">过期时间</param>
		public static void Add(string info,object val, DateTime time)
		{
			string[] 
				arr = info.Split('.');

			try
			{
                HttpContext.Current.Response.Cookies[arr[0]][arr[1]] = HttpUtility.UrlEncode(val.ToString(), Encoding.GetEncoding("utf-8"));
            }
			catch ( Exception )
			{
				HttpCookie Cookies = new HttpCookie(arr[0]);
                Cookies.Values[arr[1]] = HttpUtility.UrlEncode(val.ToString(), Encoding.GetEncoding("utf-8"));
				Cookies.Expires = time;
                HttpContext.Current.Response.Cookies.Add(Cookies);
			}

		}

		/// <summary>
		/// 新增Cookies
		/// </summary>
		/// <param name="name">项目名</param>
		/// <param name="listCont">键，值 [key,value]</param>
		/// <param name="time">过期时间</param>
		public static void Add(string name, Dictionary<string,string> listCont, DateTime time)
		{

            HttpCookie Cookies = new HttpCookie(name);
            foreach (var dic in listCont) {
                Delete($"{name}.{dic.Key}");
                Cookies.Values[dic.Key] = HttpUtility.UrlEncode(dic.Value, Encoding.GetEncoding("utf-8"));
            }
				
			Cookies.Expires = time;

			HttpContext.Current.Response.Cookies.Add(Cookies);
		}

		/// <summary>
		/// 修改Cookie
		/// </summary>
		/// <param name="name">项目名</param>
		/// <param name="list">键，值</param>
		public static void Update(string name, Dictionary<string,string> list)
		{
			HttpCookie Cookie = new HttpCookie(name);
			foreach ( var self in list ) 
				Cookie[self.Key] = HttpUtility.UrlEncode(
					self.Value, 
					Encoding.GetEncoding("utf-8")
					);
		}

		/// <summary>
		/// 获取Cookie
		/// </summary>
		/// <param name="info">标题和关键字 [Project.Name]</param>
		/// <returns></returns>
		public static string Get(string info)
		{
			string[]
				arr = info.Split('.');

			try
			{
                return HttpUtility.UrlDecode(
                    HttpContext.Current.Request.Cookies[arr[0]][arr[1]],
                    Encoding.GetEncoding("UTF-8")
                    );
            }
			catch ( Exception e )
			{
				Log.AddErr(e.Message);
				return "";
			}
			
		}

		/// <summary>
		/// 删除Cookie
		/// </summary>
		/// <param name="info">标题和关键字 [Project.Name]</param>
		public static void Delete(string info)
		{
			string[]
				arr = info.Split('.');

            if (arr.Length < 2)
            {
                HttpCookie Cookie = new HttpCookie(info);
                Cookie.Expires = DateTime.Now.AddHours(-1);
                HttpContext.Current.Response.Cookies.Add(Cookie);
            }
            else {
                HttpContext.Current.Response.Cookies[arr[0]][arr[1]] = null;
            }
			
		}

		/// <summary>
		/// 删除Cookie
		/// </summary>
		/// <param name="name">项目名</param>
		/// <param name="list">键名</param>
		public static void Delete(string name, List<string> list)
		{
			HttpCookie Cookie = new HttpCookie(name);
			foreach ( var self in list )
				Cookie.Values[self] = null;
            HttpContext.Current.Response.Cookies.Add(Cookie);
        }
    }
}
